import router from './router'
import store from './store'
import {getToken,setPath,saveCookie,removeCookie,saveStorage} from '@/utils/auth'
import qs from 'qs'
import {qqCheck} from '@/api/login'
function loginLogic(user,to,from,next){
	if(to.path === '/login' || to.name === 'register'){
		if(!user.logined){
			//记住跳到登录页之前的页面路径
			if(from.path !== '/register' && from.path !== '/forgetPassword' && from.path !== '/login' && from.name){
				setPath(from.path)
			}
			if(from.name){
				if(from.name === 'room'){
					saveCookie('1234tv-login-mode-op',store.getters.loginModeOp)
				}else{
					removeCookie('1234tv-login-mode-op')
				}
			}
			next()
		}else{
			if(!window.is_official){
				next({ path: '/' })
			}else{
				if(window.eng_name){
					next({ path: `/${window.eng_name}` })
				}else{
					next({ path: `/404` })
				}
			}
		}
	}else{
		if(to.name === 'FamilyHall1MoreList'){
			if(to.params.type === 'all' || to.params.type === 'password'){
				next()
			}else{
				next({ path: '/404' })
			}
		}else{
			if(to.name === 'recharge'){
				// if(from.name && from.name === 'room'){
					next()
				// }else{
				// 	next({ path: '/' })
				// }
			}else{
				next()
			}
		}
	}
}
router.beforeEach((to, from, next) => {
	console.log(to)
	//第三方路由跳转
	if(window.is_official){
		if((to.name === 'familyHall' || to.name === 'room' || to.name === 'home') && window.eng_name && window.eng_name !== to.params.eng_name){
			next({ path: `/${window.eng_name}`,replace:true})
		}
	}
	var _hmt = _hmt || [];
	(function() {
		//每次执行前，先移除上次插入的代码
		document.getElementById('baidu_tj') && document.getElementById('baidu_tj').remove();
		var hm = document.createElement("script");
		hm.src ="https://hm.baidu.com/hm.js?ac4a1392ff2f77ba868b986877678e47";
		hm.id ="baidu_tj"
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(hm, s);
	})();
	//从房间出来关闭聊天服务器
	if(to.path === '/v1/auth/users/qqCallback' || to.path === '/v1/auth/users/wxCallback'){
		if(to.query.code){
			let type
			if(to.path === '/v1/auth/users/qqCallback'){
				type = 'qq'
			}else if(to.path === '/v1/auth/users/wxCallback'){
				type = 'wx'
			}
			let parsedUrl = qs.parse(window.location.href.split('?')[1])
			store.dispatch('ThirdPartLogin',{parsedUrl,type}).then(res=>{
				if(res.data.open_id && type === 'wx'){
					saveStorage('openid',res.data.open_id)
				}
				if('uid' in res.data && String(res.data.uid).length>8){
					next({replace: true})
				}else{
					next({path:parsedUrl.return_url,replace:true})
				}
			}).catch(err=>{
				next({path:parsedUrl.return_url,replace:true})
			})
		}else{
			if(!window.is_official){
				next({ path: '/' })
			}else{
				if(window.eng_name){
					next({ path: `/${window.eng_name}` })
				}else{
					next({ path: `/404` })
				}
			}
		}
	}else{
		if(to.name === 'recharge'){
			next()
		}else{
			if(store.getters.user){
				loginLogic(store.getters.user,to,from,next)
			}else{
				store.dispatch('GetUserInfo').then(res=>{
					loginLogic(res.data.user,to,from,next)
				}).catch(err=>{
					next()
					console.log(err)
				})
			}
		}
	}
})